﻿@{
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<style type="text/css">
    #tab_type {
        border-collapse: collapse;
        width: 100%;
    }

    #tab_type, #xtd, #xth {
        border: 1px solid #E8E8E8;
        text-align: center;
    }
</style>
<div class="">
    <div class="ibox-content">
        <div class="jqGrid_wrapper">
            <div class="col-sm-12 jqGrid-toolbar">
                <form class="form-inline search-form" id="search-form">
                    <div class="form-group">
                        <label for="exampleInputEmail2">角色名</label>
                        <input type="text" class="form-control" id="search-entername" name="role_name">
                    </div>
                    <button type="submit" class="btn btn-primary">
                        <i class="glyphicon glyphicon-search"></i>查询
                    </button>
                </form>
                <div class="btn-group " role="group">
                    <button class="btn btn-primary" id="btn-add">
                        <i class="glyphicon glyphicon-plus"></i>新增
                    </button>
                    <button class="btn btn-success" id="btn-edit">
                        <i class="glyphicon glyphicon-pencil"></i>编辑
                    </button>
                    <button class="btn btn-danger" id="btn-del">
                        <i class="glyphicon glyphicon-trash"></i>删除
                    </button>
                </div>
            </div>
            <table id="grid"></table>
            <div id="grid_pager"></div>
        </div>
    </div>
</div>

<div class="modal fade" id="form-modal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title">角色信息</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" method="post" action="/Admin/SaveRole">
                    <div class="modal-body">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">角色名：</label>
                            <div class="col-sm-9">
                                <input type="text" name="role_name" class="form-control" placeholder="请输入角色名" required maxlength="50">
                                <input type="hidden" name="role_id" class="form-control">
                            </div>
                        </div>

                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="btn-save">保存</button>
            </div>
        </div>
    </div>
</div>

<div id="form-config-dialog" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
        </div>
    </div>
</div>

@section BodyFooter{
    <script>
        $(document).ready(function () {
            $("#grid").jqGrid({
                url: '/Admin/RoleQuery',
                colNames: ["ID", "角色名", "所属企业", "创建时间", "创建人", "更新时间", "更新人", "权限配置"],
                colModel: [{
                    name: "role_id",
                    index: "role_id",
                    width: 60,
                    key: true,
                    sorttype: "int"
                },
                {
                    name: "role_name",
                    index: "role_name",
                    sortable: false,
                    width: 90
                },
                {
                    name: "enter_name",
                    index: "enter_name",
                    sortable: false,
                    width: 100
                },
                {
                    name: "create_time",
                    index: "create_time",
                    width: 120,
                    formatter: 'date',
                    formatoptions: { newformat: 'Y-m-d H:i:s' }
                },
                {
                    name: "create_user",
                    index: "create_user"
                },
                {
                    name: "update_time",
                    index: "update_time",
                    width: 120,
                    formatter: 'date',
                    formatoptions: { newformat: 'Y-m-d H:i:s' }
                },
                {
                    name: "update_user",
                    index: "update_user",
                    sortable: false
                },
                {
                    name: "role_id",
                    index: "role_id",
                    width: 60,
                    sortable: false,
                    formatter: renderConfigCol
                }],
                caption: "角色信息"
            });
            $("#grid").adaptGridSizeInWindow();
        });

        $('#btn-add').click(function () {
            $('#form-modal').toggleModal(true, ' 新 增 ');
            $('#form-modal form').bindData(null);
        });

        $('#btn-edit').click(function () {
            var row = $("#grid").getGridSelectedData();
            if (!row)
                return;
            $('#form-modal form').bindData(row);
            $('#form-modal').toggleModal(true, ' 编 辑 ');
        });

        $('#btn-del').click(function () {
            var ids = $("#grid").jqGrid('getGridParam', 'selarrrow');
            if (ids.length == 0)
                return;
            $.confirm('确认删除数据？', function () {
                $.postJsonAjax('/Admin/DeleteRole', { ids: ids.join(',') }, function (data) {
                    $("#grid").trigger("reloadGrid");
                    if (data.Code == 1) {
                        toastr.info('数据删除成功');
                    } else {
                        toastr.error(data.Message);
                    }

                });
            });
        });
        $.validator.setDefaults({
            submitHandler: function (form) {
                $(form).ajaxSubmit(function (d) {
                    $("#grid").trigger("reloadGrid");
                    $('#form-modal').toggleModal(false);
                    toastr.info('数据保存成功');
                });
            }
        });

        $('#form-modal form').validate()

        $('#btn-save').click(function (form) {
            $('#form-modal form').submit();
        });

        $('#search-form').submit(function () {
            var data = $(this).serializeArray();
            var postData = {};
            $.each(data, function (i, n) {
                postData[n.name] = n.value;
            });

            $("#grid").jqGrid('setGridParam', {
                postData: postData, //发送数据 
                page: 1
            }).trigger("reloadGrid"); //重新载入 
            return false;
        });


    </script>
    <script>
        function renderConfigCol(cellvalue, options, rowObject) {
            var role_id = rowObject['role_id'];
            var role_name = rowObject['role_name'];
            return '<button type="button" class="btn btn-primary btn-xs config-icon-item" data-enterid="' + role_id + '" data-entername="' + role_name + '"><i class="fa fa-cog"></i><span> 权限配置</span></button>';
        }

        $(function () {
            $(document).on('click', '.config-icon-item', function () {
                var url = '/Admin/PermissionConfig/' + $(this).attr('data-enterid');
                var title = $(this).attr('data-entername');
                $('#form-config-dialog .modal-content').load(url, function () {
                    $('#form-config-dialog').toggleModal(true, '配置角色： ' + title);

                });
            });

            $('#form-config-dialog').on('click', '#btn-config-save', function () {
                $('#form-config-dialog form').ajaxSubmit(function (d) {
                    if (d.Code == 1) {
                        $('#form-config-dialog').toggleModal(false);
                        toastr.info('数据保存成功');
                    } else {
                        toastr.warning(d.Message);
                    }
                });
            });
        });
    </script>
}

